﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLBH.DTO;
using System.Text.RegularExpressions;

namespace Module.Category
{
    public partial class EditProvider : Form
    {
        private NhaCCDTO providerEdit;

        public NhaCCDTO ProviderEdit
        {
            get { return providerEdit; }
            set 
            { 
                providerEdit = value;
                if (providerEdit!=null)
                {
                    textBoxAddress.Text = providerEdit.DienChi;
                    textBoxEmail.Text = providerEdit.Email;
                    textBoxPhoneNumber.Text = providerEdit.DienThoai;
                    textBoxName.Text = providerEdit.TenNCC;
                }
            }
        }
        public EditProvider()
        {
            InitializeComponent();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (!ValidInput())
            {
                return;
            }
            //ValidInput();

            providerEdit.DienChi = textBoxAddress.Text;
            providerEdit.Email = textBoxEmail.Text;
            providerEdit.DienThoai = textBoxPhoneNumber.Text;
            providerEdit.TenNCC = textBoxName.Text;

            this.DialogResult = DialogResult.OK;
            this.Close();
        }
        private bool ValidInput()
        {
            if (!CheckEmailFromat(textBoxEmail.Text))
            {
                MessageBox.Show("Địa Chỉ Email Không Đúng!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBoxEmail.SelectAll();
                textBoxEmail.Focus();
                return false;
            }
            if (!CheckPhoneNumber(textBoxPhoneNumber.Text))
            {
                MessageBox.Show("Số Điện Thoại Không Đúng!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBoxPhoneNumber.SelectAll();
                textBoxPhoneNumber.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(textBoxName.Text))
            {
                MessageBox.Show("Bạn Cần Nhập Tên Khách Hàng!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBoxName.SelectAll();
                textBoxName.Focus();
                return false;
            }
            return true;
        }
        private bool CheckEmailFromat(string email)
        {
            string patern = @"^[\w-]+@([\w- ]+\.)+[\w-]+$";
            Regex reg = new Regex(patern);
            return reg.IsMatch(email);
        }
        private bool CheckPhoneNumber(string phoneNumber)
        {
            string patern1 = @"\d{10}";
            string patern2 = @"\d{11}";

            Regex reg1 = new Regex(patern1);
            Regex reg2 = new Regex(patern2);

            return reg1.IsMatch(phoneNumber) || reg2.IsMatch(phoneNumber);

        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
